#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

extern int errno;

int main(int argc, char *argv[])
{

	if (argc < 4) {
		fprintf(stderr,
			"compile <type_id> <input_file> [output_file]\n");
		return 1;
	}
	static const char *src_exts[] = { "",
		".c",
		".cpp",
		".java",
	};

	int src_exts_idx = atoi(argv[1]);

	switch (src_exts_idx) {

	case 1:
		//NOTE -static to ensure chroot when load shared library is available
		execl("/usr/bin/gcc", "gcc", "-static", "-v", "-Wall", "-o",
		      argv[3], argv[2], (char *)NULL);
		//execlp("gcc","gcc","divide.c",(char *)NULL);
		break;
	case 2:
		execl("/usr/bin/g++", "g++", "-v", "-Wall", "-o", argv[3],
		      argv[2], (char *)NULL);
		break;
	case 3:
		execl("/usr/bin/javac", "javac", argv[2], (char *)NULL);
		break;
	default:
		fprintf(stderr, "error type_id:%s\n", argv[1]);
		return 2;
	}

	fprintf(stderr, "execl returned, error:%s\n", strerror(errno));
	return 3;
}
